package com.wanxi.service.impl;

import com.wanxi.dao.CompanyDao;
import com.wanxi.dao.impl.CompanyDaoImpl;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.CompanyModel;
import com.wanxi.service.CompanyService;
import redis.clients.jedis.Jedis;

public class CompanyServiceImpl implements CompanyService {
    @Override
    public ResultDTO getCompanyModel() {
        /**
         * 1.首先查看redis是否有数据
         * 2.如果有，则取redis数据，并封装到model
         * 3.如果没有，则取mysql数据，存入redis
         */
        Jedis jedis = new Jedis();
        boolean isHave = jedis.exists("logo");
        CompanyModel companyModel = new CompanyModel();
        if (isHave) {
//            redis里面有数据，则数据来源于redis
            companyModel = CompanyModel.builder()
                    .logo(jedis.get("logo"))
                    .net(jedis.get("net"))
                    .phone(jedis.get("phone"))
                    .address(jedis.get("address"))
                    .build();

        } else {
            CompanyDao companyDao = new CompanyDaoImpl();
            companyModel = companyDao.getCompanyModel();
            jedis.set("logo", companyModel.getLogo());
            jedis.set("phone", companyModel.getPhone());
            jedis.set("net", companyModel.getNet());
            jedis.set("address", companyModel.getAddress());
        }
//        Jedis<String,String>
//        Jedis<Object,Object>
        return ResultDTO.builder()
                .data(companyModel)
                .build();
    }
}
